package com.jichangxiu.framework.utils;

import com.jichangxiu.common.constant.Constants;
import com.jichangxiu.common.utils.ObjUtils;
import com.jichangxiu.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Objects;

@Slf4j
public class RequestUtils {

    public static void unifiedResult() {
        log.debug("【AspectUtils】请求【" + ServletUtils.getRequestUri() + "】开启了请求统一返回值");
        Objects.requireNonNull(ServletUtils.getRequest()).setAttribute(Constants.UNIFIED_RETURN_VALUE, Constants.UNIFIED_RETURN_VALUE);
    }

    public static void removeBlank(ProceedingJoinPoint proceedingJoinPoint) {
        log.debug("【AspectUtils】请求【" + ServletUtils.getRequestUri() + "】开启了请求参数去空格");
        removeParameterBlank(proceedingJoinPoint);
    }

    private static void removeParameterBlank(ProceedingJoinPoint proceedingJoinPoint) {
        for (Object arg : proceedingJoinPoint.getArgs()) {
            try {
                ObjUtils.trimBeanStr(arg);
            } catch (Exception ex) {
                throw new RuntimeException("【AspectUtils】请求地址【" + ServletUtils.getRequestUri() + "】去除请求参数空格失败，失败参数为【" + arg + "】", ex);
            }
        }
    }

}
